/*
    Contact info:
    bhepple@freeshell.org
    http://bhepple.freeshell.org/scrmgr

    Copyright (C) 1991-2008 Bob Hepple

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; see the file COPYING.  If not, write to
    the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
    Boston, MA 02111-1307, USA.

*/
/*
 * Build with cc -o pcurse -lncurses pcurse.c 
 */
#ifdef USE_NCURSES
#include <ncurses.h>
#else
#include <curses.h>
#endif

int	current_line = 0;

void prep_for_io(void)
{
    int		row, col;

    getyx(stdscr, row, col);
    if (row == LINES - 1) {
        move(0, 0);
        deleteln();
        move(LINES - 1, 0);
    } else
        move(row + 1, 0);
    row = col;
    addstr("Hit a key (^C to end) ");
    refresh();
}

char names[][4] = {
    "nul",
    "soh",
    "stx",
    "etx",
    "eot",
    "enq",
    "ack",
    "bel",
    "bs ",
    "ht ",
    "nl ",
    "vt ",
    "np ",
    "cr ",
    "so ",
    "si ",
    "dle",
    "dc1",
    "dc2",
    "dc3",
    "dc4",
    "nak",
    "syn",
    "etb",
    "can",
    "em ",
    "sub",
    "esc",
    "fs ",
    "gs ",
    "rs ",
    "us "
};

void process_keystroke(i)
    int i;
{
    printw("%d 0x%x 0%o ", i, i, i);
    
    switch(i) {
#ifndef NO_TERMINFO
    case KEY_DOWN:		addstr("KEY_DOWN");		break;
    case KEY_UP:		addstr("KEY_UP");		break;
    case KEY_LEFT:		addstr("KEY_LEFT");		break;
    case KEY_RIGHT:		addstr("KEY_RIGHT");		break;
    case KEY_HOME:		addstr("KEY_HOME");		break;
    case KEY_BACKSPACE:		addstr("KEY_BACKSPACE");	break;
    case KEY_DL:		addstr("KEY_DL");		break;
    case KEY_IL:		addstr("KEY_IL");		break;
    case KEY_DC:		addstr("KEY_DC");		break;
    case KEY_IC:		addstr("KEY_IC");		break;
    case KEY_EIC:		addstr("KEY_EIC");		break;
    case KEY_CLEAR:		addstr("KEY_CLEAR");		break;
    case KEY_EOS:		addstr("KEY_EOS");		break;
    case KEY_EOL:		addstr("KEY_EOL");		break;
    case KEY_SF:		addstr("KEY_SF");		break;
    case KEY_SR:		addstr("KEY_SR");		break;
    case KEY_NPAGE:		addstr("KEY_NPAGE");		break;
    case KEY_PPAGE:		addstr("KEY_PPAGE");		break;
    case KEY_STAB:		addstr("KEY_STAB");		break;
    case KEY_CTAB:		addstr("KEY_CTAB");		break;
    case KEY_CATAB:		addstr("KEY_CATAB");		break;
    case KEY_ENTER:		addstr("KEY_ENTER");		break;
    case KEY_SRESET:		addstr("KEY_SRESET");		break;
    case KEY_RESET:		addstr("KEY_RESET");		break;
    case KEY_PRINT:		addstr("KEY_PRINT");		break;
    case KEY_LL:		addstr("KEY_LL");		break;
    case KEY_END:		addstr("KEY_END");		break;
	
#ifdef KEY_SLEFT
    case KEY_SLEFT:		addstr("KEY_SLEFT");		break;
    case KEY_SRIGHT:		addstr("KEY_SRIT");		break;
    case KEY_SPREVIOUS:		addstr("KEY_SPREVIOUS");	break;
    case KEY_SNEXT:		addstr("KEY_SNEXT");		break;
    case KEY_SHOME:		addstr("KEY_SHOME");		break;
    case KEY_SBEG:		addstr("KEY_SBEG");		break;
    case KEY_SEND:		addstr("KEY_SEND");		break;
#endif
#endif    
	
    default:		    
	if (i & 0x80) {
	    printw("ALT-");
	    i &= ~0x80;
	}
	if (i < ' ')
	    printw("^%c %s", i + 0100, names[i]);
	else if (i <= '~')
	    printw("%c ", i);
	else if ((i >= KEY_F0) && (i <= KEY_F0 + 12))
	    printw("KEY_F%d", i - KEY_F0);
	else
	    break;
    }
#ifdef SM_X11
    if (i == 3)
	xsmio_term();
#endif
    clrtoeol();
    prep_for_io();
}

int main(argc, argv)
    int		argc;
    char	**argv;
{
    int		i;

#ifdef SM_X11
    xsmio_init(&argc, argv);
#endif
    initscr();
    raw(); /* Instead of cbreak() ... passes ^C etc through instead of giving interrupt */
    nonl();
    noecho();

#ifndef NO_TERMINFO
    meta(stdscr, 1);
    keypad(stdscr, 1);
#endif
    
    move(current_line++, 0);
    printw("Lines = %d, Columns = %d", LINES, COLS);
    move(current_line, 0);
    clrtobot();
    prep_for_io();
#ifdef SM_X11
    xmain_loop(process_keystroke);
#else
    i = 0;
    while (i != 3) {
        i = getch();
        if (i & 0x80)
            i &= 0xff;
        process_keystroke(i);
    }
#endif
    endwin();
    return(0);
}

/* For emacs:
 * Local Variables:
 * tab-width: 8
 * End:
 */
